$(function($) {

	$("#lpnNo").focus();

	var bindLpnUrl = '../v2_pallet/ajaxPalletByLpnNo';
	var checkOrderUrl = '../v2_pallet/ajaxCheckOrderInfo';

	$("#lpnNo").change(function() {
		var shelf = this;
		var msg_lpnNo = $.trim(shelf.value);
		if (null == msg_lpnNo || msg_lpnNo == '' || msg_lpnNo.trim() == '') {
			$("#lpnNo").select().focus();
			$("#msg_lpnNo").html("请录入LPN编号!");
			return;
		} else {
			$.ajax({
				type : 'post',
				url : bindLpnUrl,
				data : {
					lpnNo : msg_lpnNo
				},
				dataType : 'json',
				success : function(data) {
					var status = data.status;
					var content = data.content;
					if (status == 0) {
						$("#lpnNo").focus().val('');
						$("#msg_lpnNo").html("无此LPN编号");
					} else if (status == 2) {
						$("#lpnNo").focus().val('');
						$("#msg_lpnNo").html("使用中");
					} else if (status == 3) {
						$("#lpnNo").focus().val('');
						$("#msg_lpnNo").html("此LPN编号已禁用");
					} else if (status == 1) {
						$("#msg_lpnNo").html("绑定LPN编号成功!");
						$("#warehouse_Code").val(content.warehouseCode);
						$("#lpnNo").attr("readOnly", "readOnly");
					}
				},
				error : function() {
					alert("网络繁忙，请稍后重试...");
				}
			});
		}
	});

	// 验证订单号
	$("#ordeno").change(function() {
		var ordeno = $("#ordeno").val().trim();
		var lpnNo = $("#lpnNo").val().trim();
		if (ordeno == '') {
			$("#ordeno").focus().val('');
			$("#msg_ordeno").html("请录入订单编号!");
			return;
		}
		if (lpnNo == '') {
			$("#ordeno").focus().val('');
			$("#msg_ordeno").html("请先绑定LPN编号!");
			return;
		}
		if (checkOrderIsLoad(ordeno)) {
			$("#ordeno").focus().val('');
			$("#msg_ordeno").html("订单已经录入!");
			return;
		}
		$.ajax({
			type : 'post',
			url : checkOrderUrl,
			data : {
				orderno : ordeno,
				lpnNo : lpnNo
			},
			dataType : 'json',
			success : function(data) {
				var msg = data.msg;
				var warehouseCode = data.workOrder.warehouseCode ;
				var warehouse_Code = $("#warehouse_Code").val();
				if(warehouseCode == warehouse_Code){
					if (msg == 'success') {
						$("#msg_ordeno").css("color", "green");
						$("#msg_ordeno").html("订单扫描成功...")
						// 显示订单信息
						showOrderInfo(data.workOrder);
					} else if (msg == 'error') {
						$("#msg_ordeno").css("color", "red");
						$("#msg_ordeno").html("无匹配的订单数据！");
					} else if (msg == 'binding') {
						$("#msg_ordeno").css("color", "red")
						$("#msg_ordeno").html("此单已绑定托盘！");
					} else if (msg == 'statuserror') {
						$("#msg_ordeno").css("color", "red");
						$("#msg_ordeno").html("订单状态异常，不允许装箱！");
					} else if(msg == 'binding2'){
						$("#msg_ordeno").css("color", "red")
						$("#msg_ordeno").html("重复订单不可绑定同一托盘！");
					}
					$("#ordeno").focus().val('');
				}else{
					$("#msg_ordeno").css("color", "red");
					$("#msg_ordeno").html("该订单不符合绑定LPN编号所属仓库！");
					$("#ordeno").focus().val('');
				}
				
			},
			error : function() {
				alert("网络繁忙，请稍后重试...");
			}
		});
	});

	$("#ordeno,#lpnNo").keydown(function() {
		$("#msg_lpnNo").html('');
		$("#msg_ordeno").html('');
	});

	/**
	 * 已加载的订单的删除操作事件绑定。
	 */
	$("table[name=orderTab]").on('click', "a[name='btn-del']", function() {
		$(this).parent().parent().remove();
	});

	/**
	 * 已加载的订单的查看操作事件绑定。
	 */
	$("table[name=orderTab]").on('click', "a[name='btn-info']", function() {
		getAjaxItems($(this).attr("orderid"));
	});

	/**
	 * 获取已装载的订单
	 */
	function getLoadOrderNos() {
		var ordernos = "";
		$("table[name=orderTab]>tbody>tr").each(
				function(idx) {
					if (idx == 0) {
						ordernos = $(this).children("td[name=orderno]").text()
								.trim();
					} else {
						ordernos = ordernos
								+ ","
								+ $(this).children("td[name=orderno]").text()
										.trim();
					}
				});
		return ordernos;
	}

	/**
	 * 检测是否已加载订单
	 */
	function checkOrderIsLoad(orderno) {
		var result = false;
		orderno = $.trim(orderno);
		$("table[name=orderTab]>tbody>tr").each(function(idx) {
			if (orderno == $(this).children("td[name=orderno]").text().trim()) {
				result = true;
				return false;
			}
		});
		return result;
	}

	// 显示装箱数据
	function showOrderInfo(workOrder) {
		$("[name='exist']").show();
		$("[name='noexist']").hide();
		if (workOrder && workOrder.order) {
			var order = workOrder.order;
			if (checkOrderIsLoad(order.ordeno)) {
				return;
			}
			var nextstr = "<tr><td align='center'>" + workOrder.customerCode
					+ "</td>" + "<td name='orderno' align='center'>"
					+ order.ordeno + "</td>" + "<td align='center'>"
					+ workOrder.goodsNum + "</td>";
			nextstr += "<td align='center'>"
					+ "<a name='btn-info' data-toggle='modal' href='#orderItemsDetail' class='btn btn-info' orderid = "
					+ order.id + " >查看</a>" + "&nbsp;"
					+ "<a name='btn-del' class='btn btn-danger'>删除</a>"
					+ "<td></tr>";
			$("table[name=orderTab] tbody").append(nextstr);
		}
	}

	/**
	 * 提交数据
	 */
	$("#btn-submit").click(function() {
		var checkArray = $("input[name='myChecked']:checked");
		var submit = 0;
		var print = 0;
		var excel = 0;
		var ordernos = "";
		for (var i = 0; i < checkArray.length; i++) {
			if (checkArray[i].value == '1') {
				submit = 1;
			} else if (checkArray[i].value == '2') {
				print = 2;
			} else if (checkArray[i].value == '3') {
				excel = 3;
			}
		}
		if (submit != 1) {
			jAlert("请勾选完成...", "温馨提示");
			return;
		}
		var lpnNo_code = $("#lpnNo").val().trim();
		
		var orderTr = $("table[name=orderTab] > tbody > tr");
		if (orderTr.length > 0) {
			$.ajax({
				type : 'post',
				url : '../v2_pallet/handle_commit',
				data : {
					lpnNo_code : lpnNo_code,
					ordernos_code : getLoadOrderNos()
				},
				dataType : 'json',
				success : function(data) {
					var msg = data.msg;
					if (msg == 'success') {
						var palletInfoList = data.palletInfoList;
						var palletInfoVo = data.palletInfoVo;
						if (excel == 3) {
							$("#queryForm").submit();
						}
						if(print == 2){
							// init打印机
							packFormPrinter("printerBody");
							//$("#btn-submit").attr("href","#selextPrinter");//打印
							$("#selextPrinter").attr("class","modal fade in");
							$("#selextPrinter").attr("aria-hidden","false");
							$("#selextPrinter").attr("style","display:block;");
							//初始化table数据
							tableHTML = "<table name = \"tableHTML\" id=\"tableHTML\" align=\"center\" border=\"1\">" ; 
							tableHTML += "<tr><th>序号</th><th>订单编号</th><th>SKU</th><th>商品名称</th><th>数量</th><th>装箱状态</th><th>装箱批次</th><th>装箱时间</th></tr>"
							for(var i = 0;i<palletInfoList.length;i++){
								tableHTML += "<tr><td>"+(i+1)+"</td><td>"+palletInfoList[i].orderNo+"</td><td>"+palletInfoList[i].skuId+"</td><td>"+palletInfoList[i].nam+"</td><td>"+palletInfoList[i].qty+"</td><td>"+(palletInfoList[i].status=="1"?"绑定中":"解绑")+"</td><td>"+palletInfoList[i].batchSeq+"</td><td>"+new Date().Format('yyyy-MM-dd hh:mm:ss')+"</td></tr>" ;
							}
							tableHTML += "</table>" ;
							$("#table_HTML").append(tableHTML);
							$("#lpnNo2").val(palletInfoVo.lpnNo);
							$("#warehouse_Code").val(palletInfoVo.warehouseCode);
							$("#name").val(palletInfoVo.name);
							$("#palletNum").val(palletInfoVo.palletNum);
							$("#username").val(palletInfoVo.username);
						}
						$("#msg_lpnNo").css("color", "red");
						$("#msg_lpnNo").html("装箱成功！");
						$("#msg_ordeno").html("");
						$("#lpnNo").removeAttr("readOnly");
						$("#lpnNo").focus().val('');
						$("#orderno_code").val('');
						$("#lpnNo_code").val('');
						$("table[name=orderTab] > tbody").empty();
					} else if (msg == 'error') {
						jAlert("装箱失败！", "温馨提示");
					} else if(msg == 'used'){
						jAlert("托盘已被使用！", "温馨提示");
					} else if (msg == 'orderused') {
						jAlert(data.content,"温馨提示");
					}
				},
				error : function() {
					alert("网络繁忙，请稍后重试...");
				}
			});
		} else {
			jAlert("请至少绑定一个订单...", "温馨提示");
		}

	});

	function printBarCode(printName, tableHTML) {
		LODOP = getLodop(document.getElementById('LODOP_OB'), document
				.getElementById('LODOP_EM'));
		//LODOP.PRINT_INIT("打印控件功能演示_Lodop功能_打印关联内容一");
		LODOP.ADD_PRINT_TABLE(90, 5, 750,900, tableHTML);
		LODOP.ADD_PRINT_TEXT(15, 300, 200, 25, "装箱单信息表");
		LODOP.SET_PRINT_STYLEA(0, "FontSize", 20);
		LODOP.ADD_PRINT_TEXT(60, 15, 200, 25, "LPN编码："+$("#lpnNo2").val());
		LODOP.SET_PRINT_STYLEA(0, "FontSize", 12);
		LODOP.ADD_PRINT_TEXT(60, 220, 200, 25, "仓库："+$("#warehouse_Code").val());
		LODOP.SET_PRINT_STYLEA(0, "FontSize", 12);
		LODOP.ADD_PRINT_TEXT(60, 420, 200, 25, "托盘名称："+$("#name").val());
		LODOP.SET_PRINT_STYLEA(0, "FontSize", 12);
		LODOP.ADD_PRINT_TEXT(60, 610, 200, 25, "托盘编码："+$("#palletNum").val());
		LODOP.SET_PRINT_STYLEA(0, "FontSize", 12);
		LODOP.ADD_PRINT_TEXT(10, 15, 200, 25, "打印时间:"+new Date().Format('yyyy-MM-dd'));
		LODOP.SET_PRINT_STYLEA(0, "FontSize", 12);
		LODOP.SET_PRINT_STYLEA(0, "LinkedItem", 1);
		LODOP.ADD_PRINT_TEXT(10, 420, 200, 25, "制表人："+$("#username").val());
		LODOP.SET_PRINT_STYLEA(0, "FontSize", 12);
		LODOP.SET_PRINT_STYLEA(0, "LinkedItem", 1);
		if (LODOP.SET_PRINTER_INDEXA(printName)) {
			LODOP.PRINT();
		}
	}
	 
	// 开始打印
	$("#beginPrint").click(function() {
		//$("#btn-submit").removeAttr("href") ;
		$("#selextPrinter").attr("class","modal in");
		$("#selextPrinter").attr("aria-hidden","true");
		$("#selextPrinter").removeAttr("style");
		var printName = $("#printerBody").val();
		var tableHTML=$("#table_HTML").html();
		printBarCode(printName, tableHTML);
	});
	//取消
	$("#noPrint").click(function(){
		//$("#btn-submit").removeAttr("href") ;
		$("#selextPrinter").attr("class","modal in");
		$("#selextPrinter").attr("aria-hidden","true");
		$("#selextPrinter").removeAttr("style");
	});
});